package com.immunology.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.immunology.core.BaseDao;
import com.immunology.core.BaseTransformer;
import com.immunology.core.GenericDao;
import com.immunology.database.DbColumns;
import com.immunology.model.Syndrome;

public class SyndromeDao extends BaseDao<Syndrome> implements
		GenericDao<Syndrome> {
	public SyndromeDao(BaseTransformer<Syndrome> transformer) {
		super(transformer, new String("SYNDROME"));
	}

	@Override
	public void insert(Syndrome syndrome) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("INSERT INTO ");
		query.append(tableName);
		query.append(" (NAME)");
		query.append(" VALUES(?)");
		try {
			PreparedStatement preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setString(DbColumns.FIRST_COLUMN,
					syndrome.getName());
			preparedStatement.executeUpdate();

			preparedStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}

	@Override
	public void update(int syndromeId, Syndrome syndrome) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("UPDATE ");
		query.append(tableName);
		query.append(" SET NAME = ?");
		query.append(" WHERE ID = ?");
		try {
			PreparedStatement preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setString(DbColumns.FIRST_COLUMN,
					syndrome.getName());
			preparedStatement.setInt(DbColumns.SECOND_COLUMN, syndrome.getId());
			preparedStatement.executeUpdate();

			preparedStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}
}
